{% extends 'base.html' %}

{% block content %}
<div class="text-center mb-12">
    <h1 class="text-4xl font-bold text-gray-800 mb-4">学生成绩数据分析结果</h1>
    <p class="text-xl text-gray-600 max-w-3xl mx-auto">
        探索学生成绩数据的关键洞察，包括相关性分析、聚类结果和预测模型评估
    </p>
</div>

<!-- 分析结果选项卡 -->
<div class="mb-8">
    <div class="border-b border-gray-200">
        <nav class="flex -mb-px" aria-label="Tabs">
            <button id="correlation-tab" class="tab-button active whitespace-nowrap py-4 px-4 border-b-2 border-primary font-medium text-sm text-primary" data-tab="correlation">
                <i class="fa fa-link mr-2"></i>相关性分析
            </button>
            <button id="clustering-tab" class="tab-button whitespace-nowrap py-4 px-4 border-b-2 border-transparent font-medium text-sm text-gray-500 hover:text-gray-700 hover:border-gray-300" data-tab="clustering">
                <i class="fa fa-object-group mr-2"></i>聚类分析
            </button>
            <button id="models-tab" class="tab-button whitespace-nowrap py-4 px-4 border-b-2 border-transparent font-medium text-sm text-gray-500 hover:text-gray-700 hover:border-gray-300" data-tab="models">
                <i class="fa fa-cogs mr-2"></i>预测模型
            </button>
            <button id="insights-tab" class="tab-button whitespace-nowrap py-4 px-4 border-b-2 border-transparent font-medium text-sm text-gray-500 hover:text-gray-700 hover:border-gray-300" data-tab="insights">
                <i class="fa fa-lightbulb-o mr-2"></i>学习建议
            </button>
            <button id="report-tab" class="tab-button whitespace-nowrap py-4 px-4 border-b-2 border-transparent font-medium text-sm text-gray-500 hover:text-gray-700 hover:border-gray-300" data-tab="report">
                <i class="fa fa-file-text-o mr-2"></i>完整报告
            </button>
        </nav>
    </div>
</div>

<!-- 分析内容区域 -->
<div class="space-y-8">
    <!-- 相关性分析 -->
    <div id="correlation-content" class="tab-content block">
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h2 class="text-2xl font-semibold mb-6 text-gray-800">相关系数矩阵</h2>
            <div class="overflow-x-auto">
                <table class="min-w-full bg-white border border-gray-200 rounded-lg">
                    <thead>
                        <tr class="bg-gray-100">
                            <th class="py-2 px-4 border-b">指标</th>
                            <th class="py-2 px-4 border-b">出勤率(attendance)</th>
                            <th class="py-2 px-4 border-b">作业(homework)</th>
                            <th class="py-2 px-4 border-b">期中考试(midterm)</th>
                            <th class="py-2 px-4 border-b">期末成绩(final_grade)</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for row in eda.corr_matrix.iterrows() %}
                        <tr class="hover:bg-gray-50">
                            <td class="py-2 px-4 border-b">{{ row[0] }}</td>
                            {% for value in row[1] %}
                            <td class="py-2 px-4 border-b text-center">{{ "%.2f"|format(value) }}</td>
                            {% endfor %}
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="mt-4 text-sm text-gray-500">
                <p>相关系数矩阵显示了各成绩指标之间的线性关系强度，取值范围为[-1, 1]，越接近1表示正相关性越强，越接近-1表示负相关性越强。</p>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-2xl font-semibold mb-6 text-gray-800">相关性热力图</h2>
            <div class="flex justify-center">
                <img src="/{{ eda.heatmap_path }}" alt="成绩相关性热力图" class="max-w-full h-auto rounded-lg shadow-md">
            </div>
            <div class="mt-4 text-sm text-gray-500">
                <p>热力图以颜色深浅直观展示了各变量间的相关程度，帮助识别影响期末成绩的关键因素。</p>
            </div>
        </div>
    </div>

    <!-- 聚类分析 -->
    <div id="clustering-content" class="tab-content hidden">
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-2xl font-semibold mb-6 text-gray-800">学生成绩K-Means聚类分析</h2>
            <div class="h-[500px] w-full">
                <iframe src="/{{ eda.cluster_path }}" width="100%" height="100%" frameborder="0"></iframe>
            </div>
            <div class="mt-4 text-sm text-gray-500">
                <p>聚类分析将学生分为3个不同群体，每个群体在出勤、作业和期中成绩方面表现出不同特征。可通过旋转3D图表从不同角度观察聚类结果。</p>
            </div>
        </div>
    </div>

    <!-- 预测模型 -->
    <div id="models-content" class="tab-content hidden">
        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-2xl font-semibold mb-4 text-gray-800">逻辑回归模型（挂科预测）</h2>
                <div class="mb-4">
                    <span class="text-sm font-medium text-gray-700">模型准确率：</span>
                    <span class="text-lg font-bold text-primary">{{ eda.models.accuracy.logistic_regression|round(4)*100 }}%</span>
                </div>
                <div class="mt-4">
                    <h3 class="text-lg font-medium mb-2 text-gray-800">特征重要性</h3>
                    <div class="space-y-2">
                        {% for feature, importance in eda.models.logistic_regression_features.items() %}
                        <div>
                            <div class="flex justify-between text-sm mb-1">
                                <span>{{ feature }}</span>
                                <span>{{ (importance*100)|round(1) }}%</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2">
                                <div class="bg-primary h-2 rounded-full" style="width: {{ importance*100 }}%"></div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-2xl font-semibold mb-4 text-gray-800">决策树模型（分数段预测）</h2>
                <div class="mb-4">
                    <span class="text-sm font-medium text-gray-700">模型准确率：</span>
                    <span class="text-lg font-bold text-secondary">{{ eda.models.accuracy.decision_tree|round(4)*100 }}%</span>
                </div>
                <div class="mt-4">
                    <h3 class="text-lg font-medium mb-2 text-gray-800">特征重要性</h3>
                    <div class="space-y-2">
                        {% for feature, importance in eda.models.decision_tree_features.items() %}
                        <div>
                            <div class="flex justify-between text-sm mb-1">
                                <span>{{ feature }}</span>
                                <span>{{ (importance*100)|round(1) }}%</span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2">
                                <div class="bg-secondary h-2 rounded-full" style="width: {{ importance*100 }}%"></div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 学习建议 -->
    <div id="insights-content" class="tab-content hidden">
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-2xl font-semibold mb-6 text-gray-800">学习建议与洞察</h2>

            <div class="mb-8">
                <h3 class="text-xl font-medium mb-3 text-gray-800">相关性洞察</h3>
                <ul class="space-y-2 text-gray-700">
                    {% for insight in insights.correlation %}
                    <li class="flex items-start">
                        <i class="fa fa-check-circle text-secondary mt-1 mr-2"></i>
                        <span>{{ insight }}</span>
                    </li>
                    {% endfor %}
                </ul>
            </div>

            <div class="mb-8">
                <h3 class="text-xl font-medium mb-3 text-gray-800">特征重要性</h3>
                <div class="overflow-x-auto">
                    {{ insights.feature_importance|safe }}
                </div>
                <p class="mt-2 text-sm text-gray-500">特征重要性显示了各因素对期末成绩预测的贡献程度。</p>
            </div>

            <div>
                <h3 class="text-xl font-medium mb-3 text-gray-800">学习建议</h3>
                <ul class="space-y-3">
                    {% for suggestion in insights.suggestions %}
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 flex items-center justify-center mt-0.5">
                            <i class="fa fa-lightbulb-o text-primary text-sm"></i>
                        </div>
                        <p class="ml-3 text-gray-700">{{ suggestion }}</p>
                    </li>
                    {% endfor %}
                </ul>
            </div>
        </div>
    </div>

    <!-- 完整报告 -->
    <div id="report-content" class="tab-content hidden">
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-2xl font-semibold mb-6 text-gray-800">完整数据分析报告</h2>
            <div class="flex justify-center mb-6">
                <a href="/{{ eda.profile_path }}" target="_blank" class="inline-flex items-center px-6 py-3 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-primary hover:bg-blue-600">
                    <i class="fa fa-file-pdf-o mr-2"></i>查看完整报告
                </a>
            </div>
            <div class="text-center text-gray-500">
                <p>完整报告包含详细的描述性统计、数据质量评估、变量分布和相关性分析。</p>
                <p class="mt-1">点击上方按钮在新窗口中打开完整报告。</p>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    // 选项卡切换功能
    document.addEventListener('DOMContentLoaded', function() {
        const tabButtons = document.querySelectorAll('.tab-button');
        const tabContents = document.querySelectorAll('.tab-content');

        tabButtons.forEach(button => {
            button.addEventListener('click', () => {
                // 移除所有活动状态
                tabButtons.forEach(btn => {
                    btn.classList.remove('active', 'border-primary', 'text-primary');
                    btn.classList.add('border-transparent', 'text-gray-500');
                });
                tabContents.forEach(content => {
                    content.classList.add('hidden');
                    content.classList.remove('block');
                });

                // 设置当前活动状态
                button.classList.add('active', 'border-primary', 'text-primary');
                button.classList.remove('border-transparent', 'text-gray-500');
                const tabId = button.getAttribute('data-tab');
                document.getElementById(`${tabId}-content`).classList.remove('hidden');
                document.getElementById(`${tabId}-content`).classList.add('block');
            });
        });
    });
</script>
{% endblock %}